package cn.pegasus.teamwork.appinterface.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.iyizhan.teamwork.base.util.ValidatorUtil;
import cn.pegasus.teamwork.appinterface.session.MySessionContext;
import cn.pegasus.teamwork.pegasus.entity.BussUserInfo;
import cn.pegasus.teamwork.pegasus.entity.BussWasherInfo;

public class BaseController {

    /**
     * 获取session
     * 
     * @param code
     * @return
     */
    protected Object getSessionAttr(String code) {
        HttpSession session = getSession();
        if (session != null && ValidatorUtil.isNotEmpty(code)) {
            return getSession().getAttribute(code);
        } else {
            return null;
        }
    }

    /**
     * 
     * 
     * @author    Buyuer
     * @version   V0.1 2015年10月30日[版本号, YYYY-MM-DD]
     * @see       获取session方法
     * @parm
     */
    protected HttpSession getSession() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String sessionId = request.getParameter("userCertificate");
        if (ValidatorUtil.isNotEmpty(sessionId)) {
            HttpSession session = MySessionContext.getInstance().getSession(sessionId);
            if (session==null) {
            	request.setAttribute("", 0);
                return request.getSession();
            }else{
                return session;
            }
        }else{
            return request.getSession();
        }
    }
    
    /**
     * 
     * @author buyuer
     * @Title: setSessionUser
     * @Description: 填充用户到session
     * @param ucenterUser
     */
    protected void  setSessionUser(BussUserInfo user) {
        getSession().setAttribute("users", user);
    }
    
    /**
     * 
     * @author buyuer
     * @Title: setSessionUser
     * @Description: 填充用户到session
     * @param ucenterUser
     */
    protected void  setSessionWasher(BussWasherInfo user) {
        getSession().setAttribute("washers", user);
    }
    
    
    /**
     * @returnBussWasherInfo
     * @author buyuer
     * @Title: getSessionUserId
     * @Description: 从session中获取已经登录的用户的id
     */
    protected Long getSessionUserId() {
    	BussUserInfo user = getSessionUser();
        if (user != null) {
            return Long.valueOf(user.getId());
        } else {
            return null;
        }
    }
    
    /**
     * @return
     * @author buyuer
     * @Title: getSessionUserId
     * @Description: 从session中获取已经登录的用户的id
     */
    protected Long getSessionWasherId() {
    	BussWasherInfo user = getSessionWasher();
        if (user != null) {
            return Long.valueOf(user.getId());
        } else {
            return null;
        }
    }
    
    /**
     * @author buyuer
     * @Title: getSessionUser
     * @Description: 获取用户
     * @return
     */
    protected BussUserInfo getSessionUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String sessionId = request.getParameter("userCertificate");
        if (ValidatorUtil.isNotEmpty(sessionId)) {
            HttpSession session = MySessionContext.getInstance().getSession(sessionId);
            if (session == null) {
                return null;
            }
            BussUserInfo user = (BussUserInfo) session.getAttribute("users");
            return user;
        }
        return null;
    }
    
    /**
     * @author buyuer
     * @Title: getSessionUser
     * @Description: 获取用户
     * @return
     */
    protected BussWasherInfo getSessionWasher() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String sessionId = request.getParameter("userCertificate");
        if (ValidatorUtil.isNotEmpty(sessionId)) {
            HttpSession session = MySessionContext.getInstance().getSession(sessionId);
            if (session == null) {
                return null;
            }
            BussWasherInfo user = (BussWasherInfo) session.getAttribute("washers");
            return user;
        }
        return null;
    }

}
